﻿using System;
using Newtonsoft.Json;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;

namespace Card.Make.Pro.Form
{
    public partial class Frm_QuickOutWhereExtend : DevExpress.XtraEditors.XtraForm
    {
        public Frm_QuickOutWhereExtend()
        {
            InitializeComponent();
        }


        public string QueryWhere { set; get; }


        private void btnOK_Click(object sender, System.EventArgs e)
        {
            var model = new
            {
                UserName = txtUserName.EditValue?.ToString(),
                UserCertifyCardNo = txtUserCertifyCardNo.EditValue?.ToString(),
                UserCareer = txtUserCareer.EditValue?.ToString(),
                CardUnit = txtCardUnit.EditValue?.ToString(),
                CardGroup = txtCardGroup.EditValue?.ToString(),
                ContactTel = txtContactTel.EditValue?.ToString(),
                CardType = txtCardType.EditValue?.ToString(),
                IsPrinted = rdoIsPrinted.SelectedIndex,
                Bind = rdoBind.SelectedIndex,
                AppId = txtAppId.EditValue?.ToString()
            };
            if (!string.IsNullOrWhiteSpace(model.UserName))
            {
                QueryWhere += $" AND UserName LIKE '%{model.UserName}%'";
            }
            if (!string.IsNullOrWhiteSpace(model.UserCertifyCardNo))
            {
                QueryWhere += $" AND UserCertifyCardNo LIKE '%{model.UserCertifyCardNo}%'";
            }
            if (!string.IsNullOrWhiteSpace(model.UserCareer))
            {
                QueryWhere += $" AND UserCareer LIKE '%{model.UserCareer}%'";
            }
            if (!string.IsNullOrWhiteSpace(model.CardUnit))
            {
                QueryWhere += $" AND CardUnit LIKE '%{model.CardUnit}%'";
            }
            if (!string.IsNullOrWhiteSpace(model.CardGroup))
            {
                QueryWhere += $" AND CardGroup LIKE '%{model.CardGroup}%'";
            }
            if (!string.IsNullOrWhiteSpace(model.ContactTel))
            {
                QueryWhere += $" AND ContactTel LIKE '%{model.ContactTel}%'";
            }
            if (!string.IsNullOrWhiteSpace(model.CardType))
            {
                QueryWhere += $" AND CardType LIKE '%{model.CardType}%'";
            }
            if (model.IsPrinted != 0)
            {
                QueryWhere += $" AND IFPrinted = '{rdoIsPrinted.Properties.Items[model.IsPrinted].Value}'";
            }
            if (model.Bind != 0)
            {
                if (rdoBind.Properties.Items[model.Bind].Value.ToString().Equals("1"))
                    QueryWhere += $" AND CardID = '{rdoBind.Properties.Items[model.Bind].Value}'";
                else
                    QueryWhere += $" AND (CardID = '' OR CardID IS NULL)";
            }

            if (!string.IsNullOrWhiteSpace(model.AppId))
            {
                QueryWhere += $" AND [AppVerID] = '{model.AppId}'";
            }

            var where = JsonConvert.SerializeObject(model);
            Properties.Settings.Default.WhereExtend = where;
            Properties.Settings.Default.Save();
            this.DialogResult = DialogResult.OK;
        }

        private void Frm_QuickOutWhereExtend_Load(object sender, System.EventArgs e)
        {
            var where = Properties.Settings.Default.WhereExtend;
            if (string.IsNullOrWhiteSpace(where))
                return;

            var obj = JsonConvert.DeserializeObject(where) as JObject;
            var userName = obj["UserName"]?.ToString();
            if (!string.IsNullOrWhiteSpace(userName))
            {
                txtUserName.EditValue = userName;
            }

            var userCertifyCardNo = obj["UserCertifyCardNo"]?.ToString();
            if (!string.IsNullOrWhiteSpace(userCertifyCardNo))
            {
                txtUserCertifyCardNo.EditValue = userCertifyCardNo;
            }

            var userCareer = obj["UserCareer"]?.ToString();
            if (!string.IsNullOrWhiteSpace(userCareer))
            {
                txtUserCareer.EditValue = userCareer;
            }

            var cardUnit = obj["CardUnit"]?.ToString();
            if (!string.IsNullOrWhiteSpace(cardUnit))
            {
                txtCardUnit.EditValue = cardUnit;
            }

            var cardGroup = obj["CardGroup"]?.ToString();
            if (!string.IsNullOrWhiteSpace(cardGroup))
            {
                txtCardGroup.EditValue = cardGroup;
            }

            var contactTel = obj["ContactTel"]?.ToString();
            if (!string.IsNullOrWhiteSpace(contactTel))
            {
                txtContactTel.EditValue = contactTel;
            }

            var cardType = obj["CardType"]?.ToString();
            if (!string.IsNullOrWhiteSpace(cardType))
            {
                txtCardType.EditValue = cardType;
            }

            var isPrinted = obj["IsPrinted"]?.ToString();
            if (!string.IsNullOrWhiteSpace(isPrinted))
            {
                rdoIsPrinted.SelectedIndex = Convert.ToInt32(isPrinted);
            }

            var bind = obj["Bind"]?.ToString();
            if (!string.IsNullOrWhiteSpace(bind))
            {
                rdoBind.SelectedIndex = Convert.ToInt32(bind);
            }

            var appId = obj["AppId"]?.ToString();
            if (!string.IsNullOrWhiteSpace(appId))
            {
                txtAppId.EditValue = appId;
            }
        }

        private void butClear_Click(object sender, EventArgs e)
        {
            txtUserName.EditValue = string.Empty;
            txtUserCertifyCardNo.EditValue = string.Empty;
            txtUserCareer.EditValue = string.Empty;
            txtCardUnit.EditValue = string.Empty;
            txtCardGroup.EditValue = string.Empty;
            txtContactTel.EditValue = string.Empty;
            txtCardType.EditValue = string.Empty;
            rdoIsPrinted.SelectedIndex = 0;
            rdoBind.SelectedIndex = 0;
            txtAppId.EditValue = string.Empty;
        }
    }
}